package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import control.CControl;

public class PDashboardPanel extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;
	private Font font = new Font("Arial",Font.PLAIN,16);
	private Font smallFont = new Font("Arial",Font.BOLD,12);
	private final int LABEL_COUNT = 3, CIRCLE_DIAMETER = 32, MAX_PANEL_WIDTH = 400;
	//private PPatternValue antistall, tractionControl;
	//private PTextValue cutIndex;
	String[] texts = {"Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6"};
	
	public PDashboardPanel(CControl cControl) {
		//antistall = new PPatternValue(cControl,"AntiStall");
		//tractionControl = new PPatternValue(cControl,"TractionControl");
		//cutIndex = new PTextValue(cControl,"ECUCutIndex");
		//cControl.addStreamObserver(this);
		
		this.setBackground(Color.white);
	}

	protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	Graphics2D g2 = (Graphics2D)g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    			RenderingHints.VALUE_ANTIALIAS_ON);
    	
    	drawStrings(g2);
    	
    	g2.setStroke(new BasicStroke(1));
	}
	
	private void drawStrings(Graphics2D g2) {
		FontRenderContext frc = g2.getFontRenderContext();
		Double fontHeight = font.getStringBounds("Hg", frc).getHeight();
		Double spacing = (getHeight()-fontHeight*LABEL_COUNT)/(LABEL_COUNT+1.);
		g2.setFont(font);
		int padding = (getWidth()-60 > MAX_PANEL_WIDTH)? (getWidth()-MAX_PANEL_WIDTH)/2 : 30;
		
		g2.drawString("Traction control:", padding, 1*(spacing.intValue()+fontHeight.intValue()));
		
		//String tractionSetting = tractionControl.value;
		//Double settingWidth = font.getStringBounds(tractionSetting, frc).getWidth();
		
		//g2.drawString(tractionSetting, 
		//			getWidth()-padding-settingWidth.intValue()-CIRCLE_DIAMETER/2+5, 
		//			1*(spacing.intValue()+fontHeight.intValue()));
		
		g2.drawString("Anti stall:", padding, 2*(spacing.intValue()+fontHeight.intValue()));
		g2.setFont(smallFont);
		/*if (antistall.value.equalsIgnoreCase("Antistall activated")) {
			g2.setPaint(Color.green);
			g2.fillOval(getWidth()-padding-CIRCLE_DIAMETER, 1*(spacing.intValue()+fontHeight.intValue())+spacing.intValue()-
					(CIRCLE_DIAMETER-fontHeight.intValue())/2+3, CIRCLE_DIAMETER, 
					CIRCLE_DIAMETER);
			g2.setPaint(Color.black);
			settingWidth = smallFont.getStringBounds("ON", frc).getWidth();
			g2.drawString("ON", getWidth()-padding-CIRCLE_DIAMETER/2-settingWidth.intValue()/2, 2*(spacing.intValue()+fontHeight.intValue())-2);
		} else if (antistall.value.equalsIgnoreCase("Antistall deactivated")) {
			g2.setPaint(Color.red);
			g2.fillOval(getWidth()-padding-CIRCLE_DIAMETER, 1*(spacing.intValue()+fontHeight.intValue())+spacing.intValue()-
					(CIRCLE_DIAMETER-fontHeight.intValue())/2+3, CIRCLE_DIAMETER, 
					CIRCLE_DIAMETER);
			g2.setPaint(Color.black);
			settingWidth = smallFont.getStringBounds("OFF", frc).getWidth();
			g2.drawString("OFF", getWidth()-padding-CIRCLE_DIAMETER/2-settingWidth.intValue()/2, 2*(spacing.intValue()+fontHeight.intValue())-2);
		}*/
		g2.setPaint(Color.black);
		g2.drawOval(getWidth()-padding-CIRCLE_DIAMETER, 1*(spacing.intValue()+fontHeight.intValue())+spacing.intValue()-
				(CIRCLE_DIAMETER-fontHeight.intValue())/2+3, CIRCLE_DIAMETER, 
				CIRCLE_DIAMETER);
		
		g2.setFont(font);
		g2.drawString("Cut index:", padding, 3*(spacing.intValue()+fontHeight.intValue()));
		
		/*String cutIndexSetting = cutIndex.value;
		settingWidth = font.getStringBounds(cutIndexSetting, frc).getWidth();
		
		g2.drawString(cutIndexSetting, 
					getWidth()-padding-settingWidth.intValue()-CIRCLE_DIAMETER/2+5, 
					3*(spacing.intValue()+fontHeight.intValue()));*/
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.repaint();
	}
}
